import axios from '../utils/axios.js'
import qs from 'qs'
import { setCookie } from '../utils/index.js'
// 获取目录文件夹
async function sendPostRequest(params, cookies) {
  try {
    return axios.post(
      `https://cloud.189.cn/api/portal/getObjectFolderNodes.action?noCache=${Math.random()}`,
      qs.stringify({ id: params.id, orderBy: 1, order: 'ASC' }),
      {
        headers: {
          'Content-Type': 'application/x-www-form-urlencoded',
          Accept: 'application/json;charset=UTF-8',
          cookie: setCookie(cookies),
        },
      },
    )
  } catch (error) {
    console.error('Error occurred:')
  }
}
// 文件重命名
async function sendRenameFolder(params, cookies) {
  //   console.log('sendRenameFolder >>>', params)
  const url = `https://cloud.189.cn/api/open/file/renameFolder.action?noCache=${Math.random()}`
  const data = { folderId: params.folderId, destFolderName: params.destFolderName }
  try {
    return axios.post(url, qs.stringify(data), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        Accept: 'application/json;charset=UTF-8',
        cookie: setCookie(cookies),
      },
    })
  } catch (error) {
    console.error('Error occurred:')
  }
}
//获取文件列表
async function sendListFiles(params, cookies) {
  const { pageSize, pageNum, mediaType, folderId, iconOption, orderBy, descending, noCache = Math.random() } = params
  const url = `https://cloud.189.cn/api/open/file/listFiles.action?${qs.stringify({
    pageSize,
    pageNum,
    mediaType,
    folderId,
    iconOption,
    orderBy,
    descending,
    noCache,
  })}`

  try {
    return axios.get(url, {
      headers: {
        accept: 'application/json;charset=UTF-8',
        'sign-type': '1',
        cookie: setCookie(cookies),
      },
    })
  } catch (error) {
    console.error('Error occurred:')
  }
}
// 获取回收站列表
function getRecycleList(cookies) {
  const url = `https://cloud.189.cn/api/portal/listRecycleBinFilesV3.action?${qs.stringify({
    pageNum: 1,
    pageSize: 3000,
    iconOption: 1,
    family: false,
    noCache: Math.random(),
  })}`

  try {
    return axios.get(url, {
      headers: {
        accept: 'application/json;charset=UTF-8',
        cookie: setCookie(cookies),
      },
    })
  } catch (error) {
    console.error('Error occurred:')
  }
}
//获取目录路径
async function sendFullPath(params, cookies) {
  const { fileId, noCache = Math.random() } = params
  // https://cloud.189.cn/api/portal/listFiles.action?noCache=0.9577226617709744&fileId=924611125163900081
  const url = `https://cloud.189.cn/api/portal/listFiles.action?${qs.stringify({
    fileId,
    noCache,
  })}`

  try {
    return axios.get(url, {
      headers: {
        accept: 'application/json;charset=UTF-8',
        'sign-type': '1',
        cookie: setCookie(cookies),
      },
    })
  } catch (error) {
    console.error('Error occurred:')
  }
}
async function createFolder(params, cookies) {
  const url = `https://cloud.189.cn/api/open/file/createFolder.action?noCache${Math.random()}`

  try {
    return axios.post(url, qs.stringify(params), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        accept: 'application/json;charset=UTF-8',
        'sign-type': '1',
        cookie: setCookie(cookies),
      },
    })
  } catch (error) {
    console.error('Error occurred:')
  }
}
async function createBatchTask(params, cookies) {
  const url = `https://cloud.189.cn/api/open/batch/createBatchTask.action?noCache${Math.random()}`
  //   console.log('createFolder params>>>', qs.stringify(params))
  try {
    return axios.post(url, qs.stringify(params), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        accept: 'application/json;charset=UTF-8',
        'sign-type': '1',
        'Browser-Id': 'f8947534d7a9fd164b23469f8341bc71',
        cookie: setCookie(cookies),
        origin: 'https://cloud.189.cn',
      },
    })
  } catch (error) {
    console.error('Error occurred:')
  }
}
function checkBatchTask(params, cookies) {
  console.log('pageNum >>>', params)
  const url = `https://cloud.189.cn/api/open/batch/checkBatchTask.action?noCache=${Math.random()}`
  try {
    return axios.post(url, qs.stringify(params), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        accept: 'application/json;charset=UTF-8',
        'sign-type': '1',
        'Browser-Id': '0a7189968638bf30042cd837e65b617f',
        cookie: setCookie(cookies),
      },
    })
  } catch (error) {
    console.error('Error occurred:', error)
  }
}
function getLoginName(cookies) {
  const url = `https://cloud.189.cn/api/open/user/getUserInfo.action?noCache=${Math.random()}`
  try {
    return axios.post(
      url,
      {},
      {
        headers: {
          'content-type': 'application/x-www-form-urlencoded',
          accept: 'application/json;charset=UTF-8',
          'sign-type': '1',
          'Browser-Id': '0a7189968638bf30042cd837e65b617f',
          cookie: setCookie(cookies),
        },
      },
    )
  } catch (error) {
    console.error('Error occurred:')
  }
}
//  获取分享目录信息
function getShareInfoAPI(params, cookies) {
  return axios
    .get(`https://cloud.189.cn/api/open/share/listShareDir.action?${qs.stringify(params)}`, {
      headers: {
        'content-type': 'application/x-www-form-urlencoded',
        accept: 'application/json;charset=UTF-8',
        cookie: setCookie(cookies),
        'sign-type': '1',
        'Browser-Id': '0a7189968638bf30042cd837e65b617f',
      },
    })
    .then((res) => {
      //   console.log('res.data11111 >>>', res)
      return Promise.resolve((res.data && res.data.fileListAO) || res)
    })
}

//  获取分享链接信息
function getShareLinkAPI(shareCode, cookies) {
  return axios.get(`https://cloud.189.cn/api/open/share/getShareInfoByCodeV2.action?shareCode=${shareCode}`, {
    headers: {
      'content-type': 'application/x-www-form-urlencoded',
      accept: 'application/json;charset=UTF-8',
      cookie: setCookie(cookies),
      'sign-type': '1',
      'Browser-Id': '0a7189968638bf30042cd837e65b617f',
    },
  })
}

//  获取分享目录信息
function getH5ShareInfoAPI(params, cookies) {
  return axios
    .get(
      `https://api.cloud.189.cn/open/share/listResourceShareDir.action?shareId=12490112599187&fileId=524603155608806897&upUserId=87d9f2ec30173d02d2fe92aca25fe864&isFolder=true&iconOption=5&orderBy=lastOpTime&descending=true&pageNum=2&pageSize=10`,
      {
        headers: {
          'content-type': 'application/x-www-form-urlencoded',
          accept: 'application/json;charset=UTF-8',
          // cookie: setCookie(cookies),
          'sign-type': '1',
          'Browser-Id': '0a7189968638bf30042cd837e65b617f',
          signature: '0e1728198505ab1be58fc5c0d6b6d7a5',
        },
      },
    )
    .then((res) => {
      return Promise.resolve(res.data.fileListAO)
    })
}

// 转存任务，url不一样
async function createBatchTaskAPI(params, cookies) {
  const url = `https://api.cloud.189.cn/open/batch/createBatchTask.action`
  try {
    return axios.post(url, qs.stringify(params), {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        accept: 'application/json;charset=UTF-8',
        'sign-type': '1',
        'Browser-Id': '0a7189968638bf30042cd837e65b617f',
        cookie: setCookie(cookies),
        signature: 'e5861f9e309295a736a294765ce6d4c3',
        timestamp: new Date().getTime(),
      },
    })
  } catch (error) {
    console.error('Error occurred:')
  }
}
const sendDingTalkMessage = (data) => {
  const url = `https://oapi.dingtalk.com/robot/send?access_token=998619e8a46e19163532432d7eaaf376c33a211a65577ed1acb3630075287a61`
  try {
    return axios.post(url, data, {
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        accept: 'application/json;charset=UTF-8',
      },
    })
  } catch (error) {
    console.error('Error occurred:')
  }
}
function getUserBriefInfo(cookies) {
  const params = {
    noCache: Math.random(),
  }
  const url = `https://cloud.189.cn/api/portal/v2/getUserBriefInfo.action?${qs.stringify(params)}`
  try {
    return axios.post(
      url,
      {},
      {
        headers: {
          'content-type': 'application/x-www-form-urlencoded',
          accept: 'application/json;charset=UTF-8',
          'sign-type': '1',
          'Browser-Id': '0a7189968638bf30042cd837e65b617f',
          cookie: setCookie(cookies),
        },
      },
    )
  } catch (error) {
    console.error('Error occurred:')
  }
}

export default {
  sendPostRequest: sendPostRequest,
  sendRenameFolder: sendRenameFolder,
  sendListFiles: sendListFiles,
  createBatchTask,
  createFolder,
  checkBatchTask,
  sendFullPath,
  getLoginName,
  getShareInfoAPI,
  createBatchTaskAPI,
  sendDingTalkMessage,
  getH5ShareInfoAPI,
  getUserBriefInfo,
  getShareLinkAPI,
  getRecycleList,
}
